import 'package:go_router/go_router.dart';
import 'package:wan_android/pages/error_page/error_page.dart';
import 'package:wan_android/pages/fav_page/fav_page.dart';
import 'package:wan_android/pages/index/index_page.dart';
import 'package:wan_android/pages/login_page/login_page.dart';
import 'package:wan_android/pages/search_page/search_page.dart';
import 'package:wan_android/pages/settings_page/settings_page.dart';
import 'package:wan_android/pages/sign_up_page/sign_up_page.dart';
import 'package:wan_android/pages/update_page/update_page.dart';
import 'package:wan_android/pages/webview_page/bridge_page.dart';
import 'package:wan_android/pages/webview_page/webview_page.dart';

final routerConfig = GoRouter(
  redirect: (context, state) {
    if (state.uri.toString() == '/') {
      return '/index';
    }
    return null;
  },

  errorBuilder: (context, state) {
    // 确保 state.error 不为空，否则提供一个默认的 GoException 实例
    final error = state.error ?? GoException('默认错误信息');
    // 移除 const 关键字
    return ErrorPage(error);
  },

  routes: [
    GoRoute(
      path: '/index',
      name: 'index',
      builder: (context, state) => const IndexPage()
    ),

    GoRoute(
      path: '/search',
      name: 'search_page',
      builder: (context, state) => const SearchPage(),
    ),

    GoRoute(
      path: '/webview',
      name: 'webview_page',
      builder: (context, state) {
        String url = state.uri.queryParameters['url']!;
        return WebviewPage(url: url);
      },
    ),

    GoRoute(
      path: '/login',
      name: 'login_page',
      builder: (context, state) {
        var username = state.extra;
        if (username != null) {
          return LoginPage(username as String);
        }
        return const LoginPage('');
      }
    ),

    GoRoute(
      path: '/sign_up',
      name: 'sign_up_page',
      builder: (context, state) => const SignUpPage()
    ),

    GoRoute(
      path: '/fav',
      name: 'fav_page',
      builder: (context, state) => const FavPage()
    ),

    GoRoute(
      path: '/settings',
      name: 'settings_page',
      builder: (context, state) => const SettingsPage()
    ),

    GoRoute(
      path: '/update',
      name: 'update_page',
      builder: (context, state) => const UpdatePage()
    ),

    GoRoute(
      path: '/bridge',
      name: 'bridge_page',
      builder: (context, state) {
        String url = state.uri.queryParameters['url']!;
        return BridgePage(url: url);
      }
    )
  ]
);


// GoRouter(
//   routes: [
//     GoRoute(
//       path: '/',
//       builder: (context, state) => const Page1Screen(),
//     ),
//     GoRoute(
//       path: '/page2',
//       builder: (context, state) => const Page2Screen(),
//     ),
//   ],
// );